| Conditions | 1 |
| Paths | 1 |
| Total Lines | 198 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 10 | jQuery(document).ready(function () { |
||
| 11 | // Start Jetpack. |
||
| 12 | BulkWP.jetpack(); |
||
| 13 | |||
| 14 | BulkWP.enableHelpTooltips( jQuery( '.bd-help' ) ); |
||
| 15 | |||
| 16 | jQuery( '.user_restrict_to_no_posts_filter' ).change( function() { |
||
| 17 | var $this = jQuery(this), |
||
| 18 | filterEnabled = $this.is( ':checked' ), |
||
| 19 | $filterItems = $this.parents( 'table' ).children().find( '.user_restrict_to_no_posts_filter_items' ); |
||
| 20 | |||
| 21 | if ( filterEnabled ) { |
||
| 22 | $filterItems.removeClass( 'visually-hidden' ); |
||
| 23 | } else { |
||
| 24 | $filterItems.addClass( 'visually-hidden' ); |
||
| 25 | } |
||
| 26 | } ); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Enable Postbox handling |
||
| 30 | */ |
||
| 31 | postboxes.add_postbox_toggles(pagenow); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Toggle the date restrict fields |
||
| 35 | */ |
||
| 36 | function toggle_date_restrict(el) { |
||
| 37 | if (jQuery("#smbd" + el + "_restrict").is(":checked")) { |
||
| 38 | jQuery("#smbd" + el + "_op").removeAttr('disabled'); |
||
| 39 | jQuery("#smbd" + el + "_days").removeAttr('disabled'); |
||
| 40 | } else { |
||
| 41 | jQuery("#smbd" + el + "_op").attr('disabled', 'true'); |
||
| 42 | jQuery("#smbd" + el + "_days").attr('disabled', 'true'); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Toggle limit restrict fields |
||
| 48 | */ |
||
| 49 | function toggle_limit_restrict(el) { |
||
| 50 | if (jQuery("#smbd" + el + "_limit").is(":checked")) { |
||
| 51 | jQuery("#smbd" + el + "_limit_to").removeAttr('disabled'); |
||
| 52 | } else { |
||
| 53 | jQuery("#smbd" + el + "_limit_to").attr('disabled', 'true'); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Toggle user login restrict fields |
||
| 59 | */ |
||
| 60 | function toggle_login_restrict(el) { |
||
| 61 | if (jQuery("#smbd" + el + "_login_restrict").is(":checked")) { |
||
| 62 | jQuery("#smbd" + el + "_login_days").removeAttr('disabled'); |
||
| 63 | } else { |
||
| 64 | jQuery("#smbd" + el + "_login_days").attr('disabled', 'true'); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Toggle user registered restrict fields |
||
| 70 | */ |
||
| 71 | function toggle_registered_restrict(el) { |
||
| 72 | if (jQuery("#smbd" + el + "_registered_restrict").is(":checked")) { |
||
| 73 | jQuery("#smbd" + el + "_registered_days").removeAttr('disabled'); |
||
| 74 | } else { |
||
| 75 | jQuery("#smbd" + el + "_registered_days").attr('disabled', 'true'); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Toggle Post type dropdown. |
||
| 81 | */ |
||
| 82 | function toggle_post_type_dropdown( el ) { |
||
| 83 | // TODO: Check why the element is not toggling even when display:none is added by JS. |
||
| 84 | if ( jQuery( "#smbd" + el + "_no_posts" ).is( ":checked" ) ) { |
||
| 85 | jQuery( "tr#smbd" + el + "-post-type-dropdown" ).show(); |
||
| 86 | } else { |
||
| 87 | jQuery( "tr#smbd" + el + "-post-type-dropdown" ).hide(); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | // hide all terms |
||
| 92 | function hideAllTerms() { |
||
| 93 | jQuery('table.terms').hide(); |
||
| 94 | jQuery('input.terms').attr('checked', false); |
||
| 95 | } |
||
| 96 | // call it for the first time |
||
| 97 | hideAllTerms(); |
||
| 98 | |||
| 99 | // taxonomy click handling |
||
| 100 | jQuery('.custom-tax').change(function () { |
||
| 101 | var $this = jQuery(this), |
||
| 102 | $tax = $this.val(), |
||
| 103 | $terms = jQuery('table.terms_' + $tax); |
||
| 104 | |||
| 105 | if ($this.is(':checked')) { |
||
| 106 | hideAllTerms(); |
||
| 107 | $terms.show('slow'); |
||
| 108 | } |
||
| 109 | }); |
||
| 110 | |||
| 111 | // date time picker |
||
| 112 | jQuery.each(BulkWP.dt_iterators, function (index, value) { |
||
| 113 | // invoke the date time picker |
||
| 114 | jQuery('#smbd' + value + '_cron_start').datetimepicker({ |
||
| 115 | dateFormat: 'yy-mm-dd', |
||
| 116 | timeFormat: 'HH:mm:ss' |
||
| 117 | }); |
||
| 118 | |||
| 119 | jQuery('#smbd' + value + '_restrict').change(function () { |
||
| 120 | toggle_date_restrict(value); |
||
| 121 | }); |
||
| 122 | |||
| 123 | jQuery('#smbd' + value + '_limit').change(function () { |
||
| 124 | toggle_limit_restrict(value); |
||
| 125 | }); |
||
| 126 | |||
| 127 | jQuery('#smbd' + value + '_login_restrict').change(function () { |
||
| 128 | toggle_login_restrict(value); |
||
| 129 | }); |
||
| 130 | |||
| 131 | jQuery('#smbd' + value + '_registered_restrict').change(function () { |
||
| 132 | toggle_registered_restrict(value); |
||
| 133 | }); |
||
| 134 | |||
| 135 | jQuery( '#smbd' + value + '_no_posts' ).change( function () { |
||
| 136 | toggle_post_type_dropdown( value ); |
||
| 137 | }); |
||
| 138 | }); |
||
| 139 | |||
| 140 | jQuery.each( BulkWP.pro_iterators, function ( index, value) { |
||
| 141 | jQuery('.bd-' + value.replace( '_', '-' ) + '-pro').hide(); |
||
| 142 | jQuery('#smbd_' + value + '_cron_freq, #smbd_' + value + '_cron_start, #smbd_' + value + '_cron').removeAttr('disabled'); |
||
| 143 | } ); |
||
| 144 | |||
| 145 | // Validate user action |
||
| 146 | jQuery('button[name="bd_action"]').click(function () { |
||
| 147 | var currentButton = jQuery(this).val(), |
||
| 148 | valid = false, |
||
| 149 | msg_key = "deletePostsWarning", |
||
| 150 | error_key = "selectPostOption"; |
||
| 151 | |||
| 152 | if (currentButton in BulkWP.validators) { |
||
| 153 | valid = BulkWP[BulkWP.validators[currentButton]](this); |
||
| 154 | } else { |
||
| 155 | if (jQuery(this).parent().prev().children('table').find(":checkbox:checked[value!='true']").size() > 0) { // monstrous selector |
||
| 156 | valid = true; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | if (valid) { |
||
| 161 | if (currentButton in BulkWP.pre_action_msg) { |
||
| 162 | msg_key = BulkWP.pre_action_msg[currentButton]; |
||
| 163 | } |
||
| 164 | |||
| 165 | return confirm(BulkWP.msg[msg_key]); |
||
| 166 | } else { |
||
| 167 | if (currentButton in BulkWP.error_msg) { |
||
| 168 | error_key = BulkWP.error_msg[currentButton]; |
||
| 169 | } |
||
| 170 | |||
| 171 | alert(BulkWP.msg[error_key]); |
||
| 172 | } |
||
| 173 | |||
| 174 | return false; |
||
| 175 | }); |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Validation functions |
||
| 179 | */ |
||
| 180 | BulkWP.noValidation = function() { |
||
| 181 | return true; |
||
| 182 | }; |
||
| 183 | |||
| 184 | BulkWP.validateSelect2 = function(that) { |
||
| 185 | if (null !== jQuery(that).parent().prev().children().find(".select2[multiple]").val()) { |
||
| 186 | return true; |
||
| 187 | } else { |
||
| 188 | return false; |
||
| 189 | } |
||
| 190 | }; |
||
| 191 | |||
| 192 | BulkWP.validateUrl = function(that) { |
||
| 193 | if (jQuery(that).parent().prev().children('table').find("textarea").val() !== '') { |
||
| 194 | return true; |
||
| 195 | } else { |
||
| 196 | return false; |
||
| 197 | } |
||
| 198 | }; |
||
| 199 | |||
| 200 | BulkWP.validateUserMeta = function() { |
||
| 201 | if (jQuery('#smbd_u_meta_value').val() !== '') { |
||
| 202 | return true; |
||
| 203 | } else { |
||
| 204 | return false; |
||
| 205 | } |
||
| 206 | }; |
||
| 207 | }); |
||
| 208 | |||
| 257 |